package b1.scenes.fight
{
	import b1.net.*;
	import b1.scenes.*;
	
	import com.greensock.*;
	import b1.controls.basic.*;
	import flash.display.Sprite;
	import flash.text.*;
	
	public class HpRestoreEvent
	{
		public var happenFlag:Boolean;
		public var decideFlag:Boolean;
		public var heirloomImg:Sprite;
		private var times:int;
		private var leftFlag:Boolean;
		private var clueMsgBgrSprite:Sprite;
		private var clueText:TextField;
		private var addHp:int;
		private var orderNum:int
		private var hpLight:BasicMc;
		
		public function HpRestoreEvent()
		{
			decideFlag = false;
			times = 0;
		}
		
		public function happen(add:int,flag:Boolean,order:int):void
		{
			this.addHp = add;
			this.leftFlag = flag;
			this.orderNum = order;
			var fight:Fight = SceneContainer.getInstance().currentScene as Fight;
			var HeirloomImg:Class = ResourceHelper.getInstance().getClassByName("Heirloom");
			heirloomImg = new HeirloomImg();
			var Light:Class = ResourceHelper.getInstance().getClassByName("HpRestoreLight");
			if(leftFlag)
			{
				hpLight = new BasicMc(Light, 4, false);
				heirloomImg.width = 100;
				heirloomImg.scaleY = heirloomImg.scaleX;
				heirloomImg.x = fight.leftAnimal.x - 40;	
				heirloomImg.y = fight.leftAnimal.y - fight.leftAnimal.height - 180;
				hpLight.x = fight.leftAnimal.x ;	
				hpLight.y = heirloomImg.y + heirloomImg.height + 5;
				hpLight.height = 7;
				hpLight.width = 5;
				fight.addChild(hpLight);
				fight.addChild(heirloomImg); 
				
				this.startSpeak();
				startMove();
			}
			else if(orderNum == 0)
			{
				hpLight = new BasicMc(Light, 4, false);
				heirloomImg.width = 100;
				heirloomImg.scaleY = heirloomImg.scaleX;
				heirloomImg.x = fight.rightAnimal.x - 40;	
				heirloomImg.y = fight.rightAnimal.y  - fight.rightAnimal.height - 180;
				hpLight.x = fight.rightAnimal.x ;	
				hpLight.y = heirloomImg.y + heirloomImg.height + 5;
				hpLight.height = 7;
				hpLight.width = 5;
				fight.addChild(hpLight);
				this.startSpeak();
				fight.addChild(heirloomImg);
				startMove();
			}
			else
			{
				hpLight = new BasicMc(Light, 4, false);
				heirloomImg.width = 100;
				heirloomImg.scaleY = heirloomImg.scaleX;
				heirloomImg.x = fight.rightAnimal2.x - 40;	
				heirloomImg.y = fight.rightAnimal2.y - fight.rightAnimal2.height - 180;
				hpLight.x = fight.rightAnimal2.x;
				hpLight.y = heirloomImg.y + heirloomImg.height + 5;
				hpLight.height = 5;
				hpLight.width = 5;
				fight.addChild(hpLight);
				this.startSpeak();
				fight.addChild(heirloomImg);
				startMove();
			}
		}
		
		private function startMove():void
		{
			var desX:int;
			var desY:int;
		    desX = heirloomImg.x;
			desY = heirloomImg.y + 30;
			TweenLite.to(heirloomImg,0.45,{x:desX, y:desY,onComplete:backMove});
			
		}
		
		private function backMove():void
		{
			var desX:int;
			var desY:int;
			desX = heirloomImg.x;
			desY = heirloomImg.y - 30;
			times ++;
			if(times < 3)
			{
				TweenLite.to(heirloomImg,0.45,{x:desX, y:desY,onComplete:startMove});
			}
			else
			{
				TweenLite.to(heirloomImg,0.45,{x:desX, y:desY, alpha: 0, onComplete:remove});
			}
		}
		
		private function remove():void
		{
			var fight:Fight = SceneContainer.getInstance().currentScene as Fight;
			if(this.heirloomImg != null ) fight.removeChild(this.heirloomImg);
			if(this.hpLight != null) fight.removeChild(hpLight);
		}
		
		private function startSpeak():void
		{
			var fight:Fight = SceneContainer.getInstance().currentScene as Fight;
			var ClueMsgBgrImage:Class = ResourceHelper.getInstance().getClassByName("DialogMsgBgrImage");
			clueMsgBgrSprite = new ClueMsgBgrImage();
			
			clueMsgBgrSprite.x = this.heirloomImg.x + 20;
			clueMsgBgrSprite.width = 180;
			clueMsgBgrSprite.height = 100;
			clueMsgBgrSprite.y = this.heirloomImg.y - clueMsgBgrSprite.height;
			if(!this.leftFlag)
			{
				clueMsgBgrSprite.scaleX = -1;
			}
			fight.addChild(clueMsgBgrSprite);
			clueText= new TextField();
			clueText.selectable = false;
        	clueText.autoSize = TextFieldAutoSize.CENTER;
        	clueText.wordWrap = false;
        	clueText.width = clueMsgBgrSprite.width * 0.8;
        	clueText.defaultTextFormat = new TextFormat("微软雅黑", 13, 0x000000);
        	clueText.text = " 传家骨显灵，生命回复\n                  " +this.addHp;
			clueText.x = clueMsgBgrSprite.x + 20 ;
			clueText.y = clueMsgBgrSprite.y + clueMsgBgrSprite.height * 0.15;
			if(!this.leftFlag)
			{
				clueText.x -= clueMsgBgrSprite.width ;
			}
			fight.addChild(clueText);
			TweenLite.delayedCall(3, closeSpeak);
		}
		
		private function closeSpeak():void
		{
			var fight:Fight = SceneContainer.getInstance().currentScene as Fight;
			if(this.leftFlag)
			{
				fight.leftAnimal.healthChange(addHp);
			}
			else if(this.orderNum == 0)
			{
				fight.rightAnimal.healthChange(addHp);
			}
			else
			{
				fight.rightAnimal2.healthChange(addHp);
			}
			if (clueText != null && clueText.parent != null)
			{
				clueText.parent.removeChild(clueText);
			}
			if (clueMsgBgrSprite != null && clueMsgBgrSprite.parent != null)
			{
				clueMsgBgrSprite.parent.removeChild(clueMsgBgrSprite);
			}
		}
	}
}